#! /usr/bin/perl -w

###################################################################################################
#
#    AUTHOR:  Johan Dahlberg
#    VERSION: Version 1.0 (10 October 2010)
#    PURPOSE: Fill a database with information in flatfile of snp-data
#    INPUT: user specified file formated with individuals as colums and genotypes for specific 
#	    snps as rows 
#    OUTPUT FILES: data to mysql database.
#
###################################################################################################

use strict;
use DBI;

my $filename = $ARGV[0];

# ---------------------------------
# This part reads the db-settnings.
# ---------------------------------

my $db_config_path = "../Perl/db_settings.txt";

#### Open input file
open DB_FILE, "<$db_config_path"
	or die "Something's wrong with the db config file!\n";

my $db_line = <DB_FILE>;		# holds the current line from the input file
my @db_conf_arr = split(/\s+/, $db_line);
my $username = shift @db_conf_arr;
my $password = shift @db_conf_arr;
my $database = shift @db_conf_arr;

close DB_FILE
        or die "Problem closing db-config file.\n";

# ----------------------------------------
# End reading db-settings.
# ----------------------------------------
### Create a connection to the database
my $dbh = DBI->connect(
    		"DBI:mysql:$database:localhost:3306",
    		$username,
    		$password
);

### Prepare the insertion statment which will later be executed in the write_to_db subfunction.
### The prepare_cached means that the handle is cached by the database and does not need to be
### recompiled each time it is called.

my $insert_handle = $dbh->prepare_cached('INSERT INTO genotype VALUES(?,?,?)')
                			or die "Couldn't prepare statement: " . $dbh->errstr;


#### OPEN OUR INPUT FILE
open INPUT_FILE, "<$filename"
	or die "Something's wrong with the input file!\n";

#-------------------------------------------------------------------------------
# This segment is used to load the first row, which contains the individuals.
#-------------------------------------------------------------------------------

my $line = <INPUT_FILE>;		# holds the current line from the input file
chomp $line;
my @individuals;
my @individuals = split(/\t/, $line);
shift(@individuals);			# remove first element of individuals which will be "individuals".

#-------------------------------------------------------------------------------
# The next row is then read and the insertion into the db can begin.
#-------------------------------------------------------------------------------

$line = <INPUT_FILE>;

my @row;			# array that will hold the current row of the file.

while (defined $line){
	chomp $line;
	@row = split(/\t/, $line);
	&write_to_db(@row);		# Call subfunction to write to db.
	$line = <INPUT_FILE>;
}


$dbh->disconnect();

#-------------------------------------------------------------------------------
# sub write_to_db
# input: array with first element being the name of the snp, and the rest being a genotype
# output: void (writes to the db)
#-------------------------------------------------------------------------------

sub write_to_db {
	
	my $snp = shift(@_);  	#Take first element of array and store

	my $genotype = shift(@_);	
	
	my $count = 0;	
	
	while (defined $genotype){
		
		#print $individuals[$count] . "\t" . $snp . "\t" .$genotype . "\n";

		$insert_handle->execute($individuals[$count], $snp, $genotype) 
            		or die "Couldn't execute insert statement.";

		$count = $count + 1;
		$genotype = shift(@_);
	
	}
}

#-------------------------------------------------------------------------------
# End of sub
#-------------------------------------------------------------------------------

